package live.testdemo.bazi.common.body;

/**
 * Created by chentz on 2017/10/19.
 */

import live.testdemo.bazi.common.constants.Sex;

/**
 * 排大运
 *
 * @author luozhuang 大师♂罗莊
 */
public class Luozhuangpaipandayun {
    public static void pringst(String[] res) {
        for (int i = 0; i < res.length; i++) {
            System.out.print(res[i]);
            System.out.print("   ");
        }
        System.out.println();
    }
//顺行排大运
    
    public String[] dayun(String nianzhu, String yuezhu, Sex isman) {
        String[] DayunStringArray = null;
        if (yuezhu == null || yuezhu.length() != 2) {
            return null;
        }
        //甲、丙、戊、庚、壬之年为阳，乙、丁、己、辛、癸之年为阴
        //阴年生男子（或阳年生女子），大运逆行否则顺行
        if (nianzhu.startsWith("甲") || nianzhu.startsWith("丙") || nianzhu.startsWith("戊") || nianzhu.startsWith("庚") || nianzhu.startsWith("庚") || nianzhu.startsWith("壬")) {
            if (isman == Sex.MAN) {//顺行
                DayunStringArray = shundayun(yuezhu);
            } else {
                DayunStringArray = nidayun(yuezhu);
            }
            
        } else {
            if (isman == Sex.MAN) {
                DayunStringArray = nidayun(yuezhu);
            } else {
                DayunStringArray = shundayun(yuezhu);
            }
            
        }
        return DayunStringArray;
    }
//逆行排大运
    
    private String[] shundayun(String yuezhu) {
        
        String[] DayunStringArray = new String[8];//取八个
        
        String[] jiazhi = BaziUtil.jiazhi;
        int start = getyuezhuStart(jiazhi, yuezhu);
        if (start == -1) {
            return null;
        } else {
            start++;
        }
        for (int i = 0; i < 8; i++) {
            DayunStringArray[i] = jiazhi[(start + i) % jiazhi.length];
        }
        
        return DayunStringArray;
        
    }
    //大运用月柱排
    
    /**
     * 找数组中月柱起始位置
     *
     * @param jiazhi
     * @param yuezhu
     * @return
     */
    public int getyuezhuStart(String[] jiazhi, String yuezhu) {
        
        int start = -1;
        for (int i = 0; i < jiazhi.length; i++) {
            if (yuezhu.equals(jiazhi[i])) {
                start = i;
                break;
            }
            
        }
        
        return start;
    }
    
    private String[] nidayun(String yuezhu) {
        String[] DayunStringArray = new String[8];//取八个
        
        String[] jiazhi = BaziUtil.jiazhi;
        int start = getyuezhuStart(jiazhi, yuezhu);
        if (start == -1) {
            return null;
        } else {
            start--;
        }
        for (int i = 0; i < 8; i++) {
            DayunStringArray[i] = jiazhi[(start - i) % jiazhi.length];
        }
        
        return DayunStringArray;
        
    }
}